package com.vortex.cloud.sample.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sun.jndi.toolkit.dir.SearchFilter;
import com.vortex.cloud.sample.domain.TaskUser;
import com.vortex.cloud.sample.domain.TaskUserInfo;
import com.vortex.cloud.sample.dto.ExcelMessageTaskUserInfoDTO;
import com.vortex.cloud.sample.mapper.ExcelMapper;
import com.vortex.cloud.sample.service.ExcelService;
import com.vortex.cloud.vfs.common.digest.MD5;
import com.vortex.cloud.vfs.common.uuid.UUIDGenerator;
import com.vortex.cloud.vfs.lite.base.dto.RestResultDTO;
import com.vortex.cloud.vfs.lite.base.dto.excel.ExcelMessageDTO;
import com.vortex.cloud.vfs.lite.base.dto.excel.ExcelReadDTO;
import com.vortex.cloud.vfs.lite.base.util.ExcelUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-15 周五
 * @time 14:09
 * @description
 */
@Service
public class ExcelServiceImpl extends ServiceImpl<ExcelMapper, TaskUserInfo> implements ExcelService {

    @Resource
    private ExcelService excelService;


    @Override
    public List<ExcelMessageDTO> importExcel(MultipartFile file, Integer startRowNum, Integer startCellNum) {

//     ExcelReadDto<CloudStaffTemplateDTO> readDto = ExcelUtil.readExcel(file, CloudStaffTemplateDTO.class, startRowNum, startCellNum);

        ExcelReadDTO<ExcelMessageTaskUserInfoDTO> readDto = ExcelUtils.readExcel(file, ExcelMessageTaskUserInfoDTO.class, startRowNum, startCellNum);
        // 如果数据格式验证不通过，则直接返回给前端
        if (CollectionUtils.isNotEmpty(readDto.getMessages())) {
            return readDto.getMessages();
        }

        System.out.println(readDto.getDatas().size() + "==============");
        System.out.println(readDto.getDatas() + "------------------------");

        // 保存数据
//        cloudStaffDao.save(staffs);
//        cloudUserService.save(users);
//        cloudUserRoleService.save(userRoles);

        List<TaskUserInfo> userInfoList = readDto.getDatas().stream().map(entity -> {
            TaskUserInfo taskUserInfo = new TaskUserInfo();
            // 把 第一个类的字段 复制到 第二类的字段上
            BeanUtils.copyProperties(entity, taskUserInfo);
            return taskUserInfo;
        }).collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(userInfoList)) {
            excelService.saveBatch(userInfoList);
        }

        System.out.println(readDto.getMessages() + "==============");

        return readDto.getMessages();
    }

//    @Override
//    public RestResultDTO<String> exportExcel(Sort sort, String fileName, String extension, String downloadUrl, String columnJson, List<T> list, HttpServletResponse response) {
//
//        LambdaQueryWrapper<TaskUserInfo> lqw = Wrappers.lambdaQuery();
//        lqw.like(StringUtils.isNotBlank(.getName()), TaskUserInfo::getName, params.getName());
//
//        List<ExcelMessageTaskUserInfoDTO> results = excelService.list(sort, this.buildQueryWrapper());
//        ExcelUtils.exportExcel("用户数据", extension, null, columnJson, results, response);
//        return RestResultDTO.newSuccess("导出成功");
//    }

}
